package com.meter.basic.controller;

import java.util.List;

import com.meter.basic.domain.BasicDevice;
import com.meter.common.core.page.TableDataInfo;
import com.meter.common.utils.ShiroUtils;
import com.meter.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.meter.common.annotation.Log;
import com.meter.common.enums.BusinessType;
import com.meter.basic.domain.BasicHouse;
import com.meter.basic.service.IBasicHouseService;
import com.meter.common.core.controller.BaseController;
import com.meter.common.core.domain.AjaxResult;
import com.meter.common.utils.StringUtils;
import com.meter.common.core.domain.Ztree;
import org.springframework.web.multipart.MultipartFile;

/**
 * 住房信息Controller
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Controller
@RequestMapping("/basic/house")
public class BasicHouseController extends BaseController
{
    private String prefix = "basic/house";

    @Autowired
    private IBasicHouseService basicHouseService;

    @RequiresPermissions("basic:house:view")
    @GetMapping()
    public String house()
    {
        return prefix + "/house";
    }

    /**
     * 查询住房信息树列表
     */
    @RequiresPermissions("basic:house:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BasicHouse basicHouse)
    {
        startPage();
        List<BasicHouse> list = basicHouseService.selectBasicHouseList(basicHouse);
        return getDataTable(list);
    }

    /**
     * @Author 王琦
     * @Description //住房信息导入
     * @Date 2020/6/19 19:56
     * @Param [file, updateSupport]
     * @return com.platform.common.core.domain.AjaxResult
     */
    @Log(title = "住房信息导入", businessType = BusinessType.IMPORT)
    @RequiresPermissions("basic:house:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<BasicHouse> util = new ExcelUtil<BasicHouse>(BasicHouse.class);
        List<BasicHouse> houseList = util.importExcel(file.getInputStream());
        String message = basicHouseService.importBaseHouse(houseList, updateSupport, ShiroUtils.getLoginName());
        return AjaxResult.success(message);
    }

    /**
     * @Author 王琦
     * @Description //导入模板
     * @Date 2020/6/19 19:56
     * @Param []
     * @return com.platform.common.core.domain.AjaxResult
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<BasicHouse> util = new ExcelUtil<BasicHouse>(BasicHouse.class);
        return util.importTemplateExcel("住房信息");
    }

    /**
     * 新增住房信息
     */
    @GetMapping(value = { "/add/{parentId}", "/add/" })
    public String add(@PathVariable(value = "parentId", required = false) Long parentId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(parentId))
        {
            mmap.put("basicHouse", basicHouseService.getById(parentId));
        }
        return prefix + "/add";
    }

    /**
     * 新增保存住房信息
     */
    @RequiresPermissions("basic:house:add")
    @Log(title = "住房信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BasicHouse basicHouse)
    {
        basicHouse.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(basicHouseService.insertBasicHouse(basicHouse));
    }

    /**
     * 修改住房信息
     */
    @RequiresPermissions("basic:house:edit")
    @GetMapping("/edit/{houseId}")
    public String edit(@PathVariable("houseId") Long houseId, ModelMap mmap)
    {
        BasicHouse basicHouse = basicHouseService.selectBasicHouseByHouseId(houseId);
        mmap.put("basicHouse", basicHouse);
        return prefix + "/edit";
    }

    /**
     * 修改保存住房信息
     */
    @RequiresPermissions("basic:house:edit")
    @Log(title = "住房信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BasicHouse basicHouse)
    {
        basicHouse.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(basicHouseService.updateBasicHouse(basicHouse));
    }

    /**
     * 删除
     */
    @RequiresPermissions("basic:house:remove")
    @Log(title = "住房信息", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{houseId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("houseId") Long houseId)
    {
        return toAjax(basicHouseService.deleteBasicHouseByHouseId(houseId));
    }

    /**
     * 选择住房信息树
     */
    @GetMapping(value = { "/selectHouseTree/{houseId}", "/selectHouseTree/" })
    public String selectHouseTree(@PathVariable(value = "houseId", required = false) Long houseId, ModelMap mmap)
    {
        if (StringUtils.isNotNull(houseId))
        {
            mmap.put("basicHouse", basicHouseService.getById(houseId));
        }
        return prefix + "/tree";
    }

    /**
     * 加载住房信息树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData()
    {
        List<Ztree> ztrees = basicHouseService.selectBasicHouseTree();
        return ztrees;
    }

    /**
     * 选择住房信息树
     */
    @GetMapping("/selectHouseAllTree")
    public String selectHouseAllTree()
    {
        return prefix + "/treeAll";
    }

    /**
     * 加载住房信息树列表
     */
    @GetMapping("/treeAllData")
    @ResponseBody
    public List<Ztree> treeAllData()
    {
        List<Ztree> ztrees = basicHouseService.selectBasicHouseAllTree();
        return ztrees;
    }
}
